package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.DialogChoosePictureBinding;

/**
 * @Author : 黄淋
 * @Time : On 2021/1/20 11:08
 * @Description : 物流 选择照片时先弹框选择“拍照”、“相册” 以及提示作用
 */
public class ChoosePictureDialog extends DialogFragment {
    private DialogClickCallBack dialogClickCallBack;
    private Context mContext;
    private Boolean Flag = false;
    private int mType;
    private int mResource;
    public final int LICENSE_MAIN = 1;   //驾驶照正页
    public final int LICENSE_DEPUTY = 2;  //驾驶证副页
    public final int QUALIFY_MAIN = 3;   //资格证正页
    public final int QUALIFY_DEPUTY = 4;  //资格证副页

    private DialogChoosePictureBinding binding;

    public ChoosePictureDialog(Context context, int resource) {
        mContext = context;
        this.mResource = resource;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.dialog_choose_picture, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    protected void initView() {
        binding.imgClose.setOnClickListener(view1 -> dismiss());
        binding.takePhoto.setOnClickListener(view1 -> {
            dialogClickCallBack.onItemClick("拍照");
            dismiss();
        });//拍照
        binding.albumChoose.setOnClickListener(view1 -> {
            dialogClickCallBack.onItemClick("从手机相册选择");
            dismiss();
        });//相册选择
    }

    public interface DialogClickCallBack {
        void onItemClick(String ids);
    }


    public ChoosePictureDialog setDialogClickCallBack(DialogClickCallBack dialogClickCallBack) {
        this.dialogClickCallBack = dialogClickCallBack;
        return this;
    }


    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        //设置对话框背景为透明
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        window.setBackgroundDrawableResource(R.color.transparent);

        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.null_img_list).fallback(R.drawable.null_img_list).error(R.drawable.null_img_list);


//        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) binding.ivDefaultTips.getLayoutParams();

        //Glide.with(mContext).load(mResource).apply(requestOptions).into(binding.ivDefaultTips);
        binding.ivDefaultTips.setImageResource(mResource);
//        if (mType == LICENSE_MAIN) {
//            Glide.with(mContext).load(R.drawable.img_license_main_no_camera).apply(requestOptions).into(binding.ivDefaultTips);
//            linearParams.width = AbScreenUtils.dp2px(mContext, 180);
//            linearParams.height = AbScreenUtils.dp2px(mContext, 110);
//            binding.ivDefaultTips.setLayoutParams(linearParams);
//        } else if (mType == LICENSE_DEPUTY) {
//            Glide.with(mContext).load(R.drawable.img_license_deputy_no_camera).apply(requestOptions).into(binding.ivDefaultTips);
//            linearParams.width = AbScreenUtils.dp2px(mContext, 180);
//            linearParams.height = AbScreenUtils.dp2px(mContext, 110);
//            binding.ivDefaultTips.setLayoutParams(linearParams);
//        } else if (mType == QUALIFY_MAIN) {
//            Glide.with(mContext).load(R.drawable.img_qualify_main_no_camera).apply(requestOptions).into(binding.ivDefaultTips);
//            linearParams.width = AbScreenUtils.dp2px(mContext, 110);
//            linearParams.height = AbScreenUtils.dp2px(mContext, 184);
//            binding.ivDefaultTips.setLayoutParams(linearParams);
//        } else if (mType == QUALIFY_DEPUTY) {
//            Glide.with(mContext).load(R.drawable.img_qualify_deputy_no_camera).apply(requestOptions).into(binding.ivDefaultTips);
//            linearParams.width = AbScreenUtils.dp2px(mContext, 110);
//            linearParams.height = AbScreenUtils.dp2px(mContext, 184);
//            binding.ivDefaultTips.setLayoutParams(linearParams);
//        }
    }


    @Override
    public void show(FragmentManager manager, String tag) {
        if (!Flag) {
            Flag = true;
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                if (manager.isDestroyed())
                    return;
            }
            try {
                //在每个add事务前增加一个remove事务，防止连续的add
                manager.beginTransaction().remove(this).commit();
                super.show(manager, tag);
            } catch (Exception e) {
                //同一实例使用不同的tag会异常，这里捕获一下
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Flag = false;
    }
}
